package ele.service.impl;


import app.nbdevices.utils.ConvertUtil;
import com.baomidou.mybatisplus.extension.api.R;
import ele.dto.SumDto;
import ele.entity.EleCycleReport;
import ele.faultHandler.EleFaultHandler;
import ele.modle.codecModle.EleReport;
import ele.modle.revModle.RevEleReportData;
import ele.service.*;
import ele.utils.EleDataDecode;
import ele.utils.EleExplain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import water.dto.DataDto;
import water.utils.WaterExplain;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service("EleService")
public class EleServiceImpl implements IEleService {


    @Autowired
    @Qualifier("EleCycleReportService")
    IEleCycleReportService svcEleCycleReport;

    @Autowired
    @Qualifier("EleCmdDataService")
    IEleCmdDataService svcEleCmdData;

    @Autowired
    EleFaultHandler eleFaultHandler;

    @Override
    public boolean saveEleCycleData(RevEleReportData revEleReportData, String deviceId) {
        EleReport eleReport= EleDataDecode.eleReportDataDecode(revEleReportData);
        EleCycleReport eleCycleReport= EleExplain.exEleCycle(eleReport);
        eleCycleReport.setInternetDeviceId(deviceId);
        eleFaultHandler.mainHandler(eleCycleReport);
        return svcEleCycleReport.save(eleCycleReport);
    }

    public static void main(String[] args) {
        RevEleReportData revEleReportData=new RevEleReportData();
        revEleReportData.setReportData("aEEAABAIGWiGKDMzMzM3iDpIMz80Mzc6fGkzNlo4UzI4MzdBMzNDMzM1MzMzMzMzMzNrFg==");
        //revEleReportData.setReportData("aEEAABAIGWiGKDMzMzM3c4NHMz80Mzc6fGkzNVk4UzI4MzdBMzNDMzM1MzMzMzMzMzOcFg==");
        EleReport eleReport= EleDataDecode.eleReportDataDecode(revEleReportData);
        EleCycleReport eleCycleReport= EleExplain.exEleCycle(eleReport);
        System.out.println(eleCycleReport);

//        RevEleReportData revEleReportData=new RevEleReportData();
//        revEleReportData.setReportData("aAEBABAIGWiGKDMzMzM3mkszMz80Mzc6e3kzOVg3UzI4MzdBMzNDMzM1MzMzMzMzMzNJFg==");
//        EleReport eleReport= EleDataDecode.eleReportDataDecode(revEleReportData);
//        EleCycleReport eleCycleReport= EleExplain.exEleCycle(eleReport);
//        System.out.println(eleCycleReport);




//        byte[] decode = ConvertUtil.getBase64Codec().decode("aCkBABAIGWiGKDMzMzM3M1czMz80Mzc6ZYQzOVg3UzI4MzdBMzNDMzM1MzMzMzMzMzMLFg==");
//        for(byte b:decode){
//            System.out.print(Integer.toHexString(Byte.toUnsignedInt(b))+" ");
//        }
    }
    private static double subtract(double v1,double v2){
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }

    @Override
    public List<DataDto> getELeDataByTime(int t, String internetDevId) {
        //获取年月日
        int[] dayMonYear = WaterExplain.
                getDayMonYear(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        List<DataDto> res=null;
        if(t==0){
            List<DataDto> waterData=svcEleCycleReport.getEleDataByDay(internetDevId,dayMonYear[0]);
            res=new LinkedList<>();
            for(int i=0;i<waterData.size()-1;i++){
                DataDto dataDto=new DataDto();
                dataDto.setTime(waterData.get(i+1).getTime());
                dataDto.setCha(subtract(waterData.get(i).getCha(),waterData.get(i+1).getCha()));
                res.add(0,dataDto);
            }
        }else if(t==1){
            res=svcEleCycleReport.getEleDataByMonth(internetDevId,dayMonYear[1]);
        }else{
            res=svcEleCycleReport.getEleDataByYear(internetDevId,dayMonYear[2]);
        }
        return res;
    }

    @Override
    public List<SumDto> getEleSum() {
        int[] dayMonYear = WaterExplain.
                getDayMonYear(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        return svcEleCycleReport.getSumEleData(dayMonYear[0]);
    }
}
